﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace PTTK11_HuongRung
{
    public partial class frmBanVe : Form
    {
        public frmBanVe()
        {
            InitializeComponent();

            Timer tmr = new Timer();
            tmr.Tick += new EventHandler(timer_Tick);
            tmr.Start();
        }

        private void frmBanVe_Load(object sender, EventArgs e)
        {
            btnDangNhap.Text = "Đăng Xuất";
            NhanVienBUS nvBus = new NhanVienBUS();
            NhanVienDTO nvDto = new NhanVienDTO();
            nvDto = nvBus.GetThongTinNhanVien(Secssion.MaNV);
            txtHoTen.Text = nvDto.TENNHANVIEN.Trim();
            LoaiNhanVienBUS lnvbus = new LoaiNhanVienBUS();
            txtMaNV.Text = lnvbus.GetTenLoaiNV(nvDto.LOAINHANVIEN);
            reloadVe();

        }
        private void reloadVe()
        {
            // comboBox1.ad
            //VeBUS bus = new VeBUS();
            //comboBox1.DataSource = bus.GetVe();
            //comboBox1.DisplayMember = "loaive";
            //comboBox1.ValueMember = "loaive";
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void radNguoiLon_CheckedChanged(object sender, EventArgs e)
        {
            if (radNguoiLon.Checked == true)
            {
                if (radSang.Checked == true)
                    lbDonGia.Text = "80000";
                if (radTrua.Checked == true)
                    lbDonGia.Text = "80000";
                if (radToi.Checked == true)
                    lbDonGia.Text = "180000";
            }
            if (radTreEm.Checked == true)
            {
                if (radSang.Checked == true)
                    lbDonGia.Text = "60000";
                if (radTrua.Checked == true)
                    lbDonGia.Text = "60000";
                if (radToi.Checked == true)
                    lbDonGia.Text = "100000";
            }
        }

        private void radSang_CheckedChanged(object sender, EventArgs e)
        {
            if (radNguoiLon.Checked == true)
            {
                if (radSang.Checked == true)
                    lbDonGia.Text = "80000";
                if (radTrua.Checked == true)
                    lbDonGia.Text = "80000";
                if (radToi.Checked == true)
                    lbDonGia.Text = "180000";
            }
            if (radTreEm.Checked == true)
            {
                if (radSang.Checked == true)
                    lbDonGia.Text = "60000";
                if (radTrua.Checked == true)
                    lbDonGia.Text = "60000";
                if (radToi.Checked == true)
                    lbDonGia.Text = "100000";
            }
        }

        private void radTrua_CheckedChanged(object sender, EventArgs e)
        {
            if (radNguoiLon.Checked == true)
            {
                if (radSang.Checked == true)
                    lbDonGia.Text = "80000";
                if (radTrua.Checked == true)
                    lbDonGia.Text = "80000";
                if (radToi.Checked == true)
                    lbDonGia.Text = "180000";
            }
            if (radTreEm.Checked == true)
            {
                if (radSang.Checked == true)
                    lbDonGia.Text = "60000";
                if (radTrua.Checked == true)
                    lbDonGia.Text = "60000";
                if (radToi.Checked == true)
                    lbDonGia.Text = "100000";
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtSoLuong.Text.Count() == 0)
            {
                MessageBox.Show("Bạn phải chọn số lượng!");
                txtSoLuong.Focus();
                return;
            }
            //else if (int.Parse(txtSoLuong.Text) > int.Parse(lbVe.Text))
            //{
            //    MessageBox.Show("Không còn đủ số lượng vé!");
            //    txtSoLuong.Focus();
            //    return;
            //}
            DateTime ngay = dateTimePicker1.Value;
            int buoi = 0;
            if (radSang.Checked == true)
                buoi = 1;
            else if (radTrua.Checked == true)
                buoi = 2;
            else if (radToi.Checked == true)
                buoi = 3;
            bool loaive = true;
            if (radTreEm.Checked == true)
                loaive = false;
            int sl = int.Parse(txtSoLuong.Text);
            VeBUS busVe = new VeBUS();

            KhachHangBUS busKH = new KhachHangBUS();
            string mathe = txtMaThe.Text;


            if (txtMaThe.Text.Count() != 0)
            {
                if (busKH.Get1KhachHang(mathe).Count == 0)
                {
                    MessageBox.Show("Mã thẻ không tồn tại!");
                    txtMaThe.Focus();
                    return;
                }
                else
                {
                    if (busVe.BanVe(ngay, buoi, loaive, sl, int.Parse(lbDonGia.Text)) == 1)
                    {
                        int diem = 0;
                        if (double.Parse(lbThanhTien.Text) >= 6000000)
                        {
                            diem = 120;
                        }
                        else if (double.Parse(lbThanhTien.Text) >= 5000000)
                        {
                            diem = 90;
                        }
                        else if (double.Parse(lbThanhTien.Text) >= 4000000)
                        {
                            diem = 70;
                        }
                        else if (double.Parse(lbThanhTien.Text) >= 3000000)
                        {
                            diem = 55;
                        }
                        else if (double.Parse(lbThanhTien.Text) >= 2000000)
                        {
                            diem = 40;
                        }
                        else if (double.Parse(lbThanhTien.Text) >= 1000000)
                        {
                            diem = 25;
                        }
                        else if (double.Parse(lbThanhTien.Text) >= 500000)
                        {
                            diem = 10;
                        }
                        if (busKH.CongDiem(txtMaThe.Text, diem) == 1)
                            MessageBox.Show("Thành công!");
                        else
                            MessageBox.Show("Cộng điểm thất baị!");
                    }
                    else
                        MessageBox.Show("Thất bại!");
                }
            }
            else
            {
                if (busVe.BanVe(ngay, buoi, loaive, sl, int.Parse(lbDonGia.Text)) == 1)
                {
                    MessageBox.Show("Thành công!");
                }
                else
                    MessageBox.Show("Thất bại!");
            }

        }

        private void txtSoLuong_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtSoLuong.Text.Count() != 0)
            {
                int soluong = int.Parse(txtSoLuong.Text);
                lbThanhTien.Text = (soluong * (int.Parse(lbDonGia.Text))).ToString();
            }
            else
                lbThanhTien.Text = "0";
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            //DateTime ngay = dateTimePicker1.Value;
            //int buoi = 0;
            //if (radSang.Checked == true)
            //    buoi = 1;
            //else if (radTrua.Checked == true)
            //    buoi = 2;
            //else if (radToi.Checked == true)
            //    buoi = 3;
            //VeBUS bus = new VeBUS();
            //int soluong = bus.GetSoLuong(ngay, buoi);
            //lbVe.Text = soluong.ToString();
            //comboBox1.DataSource = bus.GetVe();
            //comboBox1.DisplayMember = "loaive";
            //comboBox1.ValueMember = "loaive";
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ThoiGian.Text = DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmQuanLy frm = new frmQuanLy();
            frm.ShowDialog();
            this.Close();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text.Trim().CompareTo("Đăng Xuất") == 0)
            {
                btnDangNhap.Text = "Đăng Nhâp";
                Secssion.MaNV = "";
                Secssion.MaKhac = "";

                txtHoTen.Text = "";
                txtMaNV.Text = "";
                this.Hide();
                frmQuanLy frm = new frmQuanLy();
                frm.ShowDialog();
                this.Close();
            }
            else
            {
                this.Hide();
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();
                this.Close();
            }
        }

    }
}
